package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.CldMaterialDetails;
import com.cleaningcloud.cld.domain.CldProject;
import com.cleaningcloud.cld.domain.CldProjectRepository;
import com.cleaningcloud.cld.domain.excelExport.ProjectRepositoryExprot;
import com.cleaningcloud.cld.domain.vo.CldTeamMemberDetailsListVo;
import com.cleaningcloud.cld.domain.vo.CldTeamMemberDetailsVo;
import com.cleaningcloud.cld.mapper.CldProjectMapper;
import com.cleaningcloud.cld.mapper.CldProjectRepositoryMapper;
import com.cleaningcloud.cld.service.ICldProjectRepositoryService;
import com.cleaningcloud.common.security.utils.CommunityTypeConversionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CldProjectRepositoryServiceImpl implements ICldProjectRepositoryService {


    @Autowired
    private CldProjectRepositoryMapper repositoryMapper;

    @Autowired
    private CldProjectMapper cldProjectMapper;

    @Override
    public List<CldProjectRepository> selectCldProjectRepositoryList(CldProjectRepository cldProjectRepository) {
        return repositoryMapper.selectCldProjectRepositoryList(cldProjectRepository);
    }

    @Override
    public List<CldMaterialDetails> selectCldMaterialDetailsList(CldMaterialDetails cldMaterialDetails) {
        //cldMaterialDetails.setCommodityType(CommunityTypeConversionUtils.conversionRegexp(cldMaterialDetails.getCommodityType()));
        if(null != cldMaterialDetails.getCommodityType()){
            cldMaterialDetails.setCommodityType(cldMaterialDetails.getCommodityType().replace('|',','));
        }
        List<CldMaterialDetails> list = repositoryMapper.selectCldMaterialDetailsList(cldMaterialDetails);
        for (CldMaterialDetails materialDetails : list) {
            materialDetails.setCommodityTypeName(CommunityTypeConversionUtils.conversion(materialDetails.getCommodityType()));
        }
        return list;
    }

    @Override
    public List<CldMaterialDetails> selectGroupConsumeList(Long wareId) {
        List<CldMaterialDetails> list = repositoryMapper.selectGroupConsumeList(wareId);
        for (CldMaterialDetails materialDetails : list) {
            if(null != materialDetails){
                materialDetails.setCommodityTypeName(CommunityTypeConversionUtils.conversion(materialDetails.getCommodityType()));
            }
        }
        return list;
    }

    @Override
    public Map<String, Object> selectCldTeamMemberDetailsList(CldProjectRepository cldProjectRepository) {
        Map<String, Object> resultMap = new HashMap<>();
        // 项目信息
        CldProject project = cldProjectMapper.selectCldProjectByProjectId(cldProjectRepository.getProjectId());
        resultMap.put("projectName", project.getProjectName());
        // 项目库存

        // 管理岗
        resultMap.put("managePost", repositoryMapper.selectManagementPositions(cldProjectRepository.getProjectId()));
        // 在岗人数 总库存 库存成本
        Map<String, Object> stockMap = repositoryMapper.selectStockByProjectId(cldProjectRepository.getProjectId());
        if (stockMap != null) {
            resultMap.putAll(stockMap);
        }
        // 消耗总量 消耗总成本
        Map<String, Object> consumeMap = repositoryMapper.selectConsumeByProjectId(cldProjectRepository);
        if (consumeMap != null) {
            resultMap.putAll(consumeMap);
        }
        // 小组明细
        List<CldTeamMemberDetailsVo> memberDetailsVos = repositoryMapper.selectCldTeamMemberDetailsList(cldProjectRepository.getProjectId());
        // 小组消耗明细
        for (CldTeamMemberDetailsVo memberDetailsVo : memberDetailsVos) {
            for (CldTeamMemberDetailsListVo cldTeamMemberDetailsListVo : memberDetailsVo.getList()) {
                cldProjectRepository.setWareId(cldTeamMemberDetailsListVo.getWareId());
                Map<String, Object> map = repositoryMapper.selectConsumeByWareId(cldProjectRepository);
                if (map != null) {
                    cldTeamMemberDetailsListVo.setConsumeNumber(Integer.parseInt(map.get("consumeNumber").toString()));
                    cldTeamMemberDetailsListVo.setConsumeMoney(map.get("consumeMoney").toString());
                }
            }
        }
        resultMap.put("groupList", memberDetailsVos);
        return resultMap;
    }

    @Override
    public List<Map<String, Object>> selectStaffStockList(CldMaterialDetails cldMaterialDetails) {
        return repositoryMapper.selectStaffStockList(cldMaterialDetails);
    }

    /**
     * @Author Shaw
     * @Description 项目仓库物资明细列表导出
     * @date: 2025-03-18 9:12
     * @param: //TODO
     */
    @Override
    public List<ProjectRepositoryExprot> exportProjectRepositoryList(ProjectRepositoryExprot projectRepositoryExprot) {
        return repositoryMapper.exportProjectRepositoryList(projectRepositoryExprot);
    }
}
